﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports DevComponents.DotNetBar
Imports TNCDAL

Public Class FrmUnitPrice
    Inherits DevComponents.DotNetBar.Office2007Form
    Dim moil As New MOilUnitPrices
    Dim id As Integer
    Dim price As Integer
    Dim dates As DateTime
    Dim des As String
    Dim TotRowCount As Integer
    Dim RowId As Integer
    Sub bindingTogrid()
        grid.AllowUserToAddRows = False
        grid.ReadOnly = True
        grid.DataSource = moil.getUnitPricesList2()
        grid.Columns(0).ReadOnly = True
        TotRowCount = grid.RowCount - 1

    End Sub
    Private Sub FrmUnitPrice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim col1 As New DataGridViewTextBoxColumn()
        'col1.HeaderText = "ID"
        'col1.DataPropertyName = "ID"
        'Me.grid.Columns.Add(col1)
        'Dim col2 As New DataGridViewTextBoxColumn()
        'col2.HeaderText = "Giá"
        'col2.DataPropertyName = "UnitPrice"
        'Me.grid.Columns.Add(col2)
        'Dim col3 As New DevComponents.DotNetBar.Controls.DataGridViewDateTimeInputColumn()
        'col3.HeaderText = "Ngày"
        'col3.DataPropertyName = "Date"
        'Me.grid.Columns.Add(col3)
        'Dim col4 As New DataGridViewTextBoxColumn()
        'col4.HeaderText = "Mô tả"
        'col4.DataPropertyName = "Description"
        'Me.grid.Columns.Add(col4)
        'grid.EditMode = DataGridViewEditMode.EditOnEnter
        'grid.AllowUserToAddRows = False
        'grid.ReadOnly = True
        bindingTogrid()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        For index = 0 To grid.Rows.Count - 2

            dates = Convert.ToDateTime(grid.Rows(index).Cells(2).Value.ToString())

            If grid.Rows(index).Cells(3).FormattedValue.ToString() <> "" Then
                des = grid.Rows(index).Cells(3).Value.ToString()
            Else : des = ""
            End If
            price = Convert.ToInt32(grid.Rows(index).Cells(1).Value)
            id = Convert.ToInt32(grid.Rows(index).Cells(0).Value)
            Dim objoil As New tblOilUnitPrice

            If Not IsDBNull(price) And Not IsDBNull(dates) Then
                objoil.ID = id
                objoil.UnitPrice = price
                objoil.Description = des
                objoil.DateChange = dates

                If id <> 0 Then
                    moil.UpdateRecord(objoil)
                Else
                    moil.InsertNewRecord(objoil)
                End If


                'mcur.InsertNewRecord(objCurr)

            End If
        Next

        bindingTogrid()
        lblMsg.Text = "Record updated successfully"
        lblMsg.Visible = True
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (MessageBox.Show("Do you really want to delete selected record?",
                    "Delete Warning", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, False) = DialogResult.Yes) Then

            For i As Integer = 0 To grid.SelectedRows.Count - 1
                RowId = grid.SelectedRows(i).Index
                id = Convert.ToInt32(grid(0, RowId).Value)

                If id <> 0 Then
                    moil.DeleteRecord(id)
                End If

            Next

        End If
        lblMsg.Text = "Record deleted successfully"
        lblMsg.Visible = True
        bindingTogrid()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        grid.AllowUserToAddRows = True
        grid.ReadOnly = False
    End Sub

    Private Sub btnEdit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        grid.ReadOnly = False
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        grid.AllowUserToAddRows = False
        grid.ReadOnly = True
        bindingTogrid()
    End Sub

    Private Sub grid_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles grid.CellPainting
        Dim dg As DataGridView = CType(sender, DataGridView)
        If e.RowIndex >= 0 Then
            If e.RowIndex <= grid.Rows.Count - 1 Then
                Dim c As Color
                If CType(dg.Rows(e.RowIndex).HeaderCell.Value, Integer) Mod 2 <> 0 Then
                    c = Color.CadetBlue
                Else
                    c = Color.White
                End If
                e.CellStyle.BackColor = c
            End If
        End If

    End Sub

    Private Sub grid_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grid.DataBindingComplete
        Dim gridView As DataGridView = CType(sender, DataGridView)

        If gridView IsNot Nothing Then
            For Each r As DataGridViewRow In gridView.Rows
                gridView.Rows(r.Index).HeaderCell.Value = (r.Index + 1).ToString()
            Next

        End If
    End Sub
End Class